<template>
  <div class="p16 flex-column-null-between container">
    <el-row>
      <el-col :span="7">
        <div class="grid-content bg-purple" @click="OptionCardClose">
          <div slot="header" class="top-from">参数分类
            <el-button style="margin-left: 50px" type="success" size="mini" icon="el-icon-refresh"
                       @click="refreshParameter">刷新
            </el-button>
          </div>
          <div style="margin-top:2px;overflow: auto;height: calc(100vh - 220px)">
            <el-tree
                :data="parameterData"
                :props="defaultProps"
                highlight-current
                default-expand-all
                :expand-on-click-node="parameterExpandOnClickNode"
                @node-click="handleNodeClick">

            </el-tree>
          </div>
        </div>
      </el-col>

      <el-col :span="16">
        <div class="grid-content bg-purple-light">
          <div slot="header" class="top-from">
            <el-col :span="12"> 分类： {{ nowCataLog.code }}</el-col>

            <el-col :span="12">
              <el-button style="margin-left: 80%" type="success" size="mini" icon="el-icon-save"
                         @click="changeParameters">保存
              </el-button>
            </el-col>
          </div>
          <div style="overflow: auto;height: calc(100vh - 220px)">
            <div v-for="(item,index) in parameterVos" :key="index">
              <p style="color: black;font-size: 12px">{{ item.description }} &nbsp&nbsp({{ item.parameterKey }})  &nbsp&nbsp 参数类型:&nbsp{{item.type}}</p>
              <el-input size="small" v-if="item.type === 'INTEGER' || item.type === 'STRING'"
                        v-model="item.defaultValue" placeholder="请输入参数值"></el-input>
              <el-checkbox v-if="item.type === 'BOOLEAN'" v-model="item.defaultValue"></el-checkbox>
              <br/><br/>
            </div>

          </div>
        </div>

      </el-col>

    </el-row>
  </div>
</template>

<script>
import {queryParameterList,changeParameters} from '@/api/parameter';

export default {

  data() {
    return {
      parameterExpandOnClickNode:false,
      parameterData: [],
      parameterLoading: false,
      defaultProps: {
        children: 'children',
        label: 'parameterCatalog'
      },
      nowCataLog: {},
      parameterVos: [],
    }

  },
  created() {
    this.queryParameterList();
  },
  methods: {
    //刷新
    refreshParameter() {
      this.queryParameterList();
      this.nowCataLog = {};
    },
    async queryParameterList() {
      let res = await queryParameterList();
      this.parameterData = res.data;
    },
    handleNodeClick(catalog) {
      if (catalog == null) {
        return;
      }
      this.nowCataLog = catalog;
      this.parameterVos = catalog.parameterVos;
      for (let index in this.parameterVos) {
        if (this.parameterVos[index].type === 'BOOLEAN') {
          this.parameterVos[index].defaultValue = JSON.parse(this.parameterVos[index].defaultValue);
        }
      }
    },
    async changeParameters() {
        let param = JSON.parse(JSON.stringify(this.parameterVos));
        for (let index in param) {
          if (param[index].type === 'BOOLEAN') {
            param[index].defaultValue = String(param[index].defaultValue);
          }
        }
        try{
          let res = await changeParameters(this.parameterVos);
          if(res.status === '200'){
            this.$message.success('保存成功')
          }else{
            this.$message.error(res.msg)
          }
        }catch{
        }
    },
    // 点击框外区域 隐藏菜单
    OptionCardClose(event) {
      var currentCli = document.getElementById("option-button-group");
      if (currentCli) {
        if (!currentCli.contains(event.target)) {
          //点击到了id为option-button-group以外的区域，就隐藏菜单
          this.optionCardShow = false;
        }
      }
    },

  }

}
</script>


<style src="../css/index.scss" lang="scss"></style>
<style lang="scss" scoped>

.folder-box {
  height: 100%;
}

.option-card-button {
  width: 100%;
  margin-left: 0;
  font-size: 12px;
  border-radius: 0;
}

.search-item {
  float: left;
  margin: 0 20px 20px 0;

  span {
    color: #606266;
    font-size: 14px;
    margin-right: 10px;
  }
}

.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  /* background: #d3dce6; */
}

.bg-purple-light {
  /* background: #e5e9f2; */
}

.top-from {
  height: 50px;
}

.grid-content {
  border: 1px solid #e1e4e7;
  border-radius: 4px;
  margin-top: 0px;
  background: #fff;
  //设置内容高度随浏览器变化
  height: calc(100vh - 160px);
  //min-height:  600px;
  overflow: auto;
  padding-top: 15px;
  padding-left: 20px;
  padding-right: 20px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

</style>
